import { ref } from 'vue'
import type { TreeNodeData } from '@arco-design/web-vue'
import {addressTree, getCategoryTrees, getEnterpriseTrees, listDeptTree, subjectDeleteTree, subjectTree} from '@/apis'

/** 部门模块 */
export function useDept(options?: { onSuccess?: () => void }) {
  const loading = ref(false)
  const deptList = ref<TreeNodeData[]>([])

  const getDeptList = async (name?: string) => {
    try {
      loading.value = true
      const res = await listDeptTree({ description: name })
      deptList.value = res.data
      options?.onSuccess && options.onSuccess()
    } finally {
      loading.value = false
    }
    deptList.value = getTreeData(deptList.value);
  }
  return { deptList, getDeptList, loading }
}


/** 部门模块 */
export function subject(options?: { onSuccess?: () => void }) {
  const loading = ref(false)
  const subjectList = ref<TreeNodeData[]>([])

  const getSubjectList = async (name?: string) => {
    try {
      loading.value = true
      const res = await subjectTree()
      subjectList.value = res.data
      options?.onSuccess && options.onSuccess()
    } finally {
      loading.value = false
    }
    subjectList.value = getTreeData(subjectList.value);
  }
  return { subjectList, getSubjectList, loading }
}


/** 部门模块 */
export function subjectDelete(options?: { onSuccess?: () => void }) {
  const loading = ref(false)
  const subjectList = ref<TreeNodeData[]>([])

  const getSubjectList = async (name?: string) => {
    try {
      loading.value = true
      const res = await subjectDeleteTree()
      subjectList.value = res.data
      options?.onSuccess && options.onSuccess()
    } finally {
      loading.value = false
    }
    subjectList.value = getTreeData(subjectList.value);
  }
  return { subjectList, getSubjectList, loading }
}

// 递归方法
const getTreeData = (data:any)=> {
  // 循环遍历json数据
  for (let i = 0; i < data.length; i++) {
    if (data[i].children.length < 1) {
      // children若为空数组，则将children设为undefined
      delete data[i].children;
    } else {
      // children若不为空数组，则继续 递归调用 本方法
      getTreeData(data[i].children);
    }
  }
  return data;
}



/** 地址模块 */
export function address(options?: { onSuccess?: () => void }) {
  const loading = ref(false)
  const addressList = ref<TreeNodeData[]>([])

  const getAddressList = async () => {
    try {
      loading.value = true
      const res = await addressTree()
      addressList.value = res.data
      options?.onSuccess && options.onSuccess()
    } finally {
      loading.value = false
    }
    addressList.value = getTreeData(addressList.value);
  }
  return { addressList, getAddressList, loading }
}


/** 企業模块 */
export function enterpriseTree(options?: { onSuccess?: () => void }) {
  const loading = ref(false)
  const enterpriseList = ref<TreeNodeData[]>([])
  const getEnterpriseList = async () => {
    try {
      loading.value = true
      const res = await getEnterpriseTrees()
      enterpriseList.value = res.data
      options?.onSuccess && options.onSuccess()
    } finally {
      loading.value = false
    }
    enterpriseList.value = getTreeData(enterpriseList.value);
  }
  return { enterpriseList, getEnterpriseList, loading }
}



/** 企業模块 */
export function categoryTree(options?: { onSuccess?: () => void }) {
  const loading = ref(false)
  const categoryList = ref<TreeNodeData[]>([])
  const getCategoryList = async () => {
    try {
      loading.value = true
      const res = await getCategoryTrees()
      categoryList.value = res.data
      options?.onSuccess && options.onSuccess()
    } finally {
      loading.value = false
    }
    categoryList.value = getTreeData(categoryList.value);
  }
  return { categoryList, getCategoryList, loading }
}

